﻿
Partial Class CheckoutPage
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If UserCart.GetCart.Count <= 0 Then
            Response.Redirect("Products.aspx")
        End If
        If Not Page.IsPostBack Then
            If HttpContext.Current.User.Identity.IsAuthenticated Then
                lblMyCustomerID.Text = CType(Session("UserObject"), User).CustomerID
                rdlChoice.SelectedValue = 0
                rdlChoice.Items(1).Enabled = False
                wizCheckout.ActiveStepIndex = 1
                ddlCardList.DataBind()
            Else
                rdlChoice.SelectedValue = 1
                CustomerInfo1.Visible = True
            End If
        End If
    End Sub

    Protected Sub rdlChoice_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles rdlChoice.SelectedIndexChanged
        If rdlChoice.SelectedValue = 0 AndAlso Not HttpContext.Current.User.Identity.IsAuthenticated Then
            CustomerInfo1.Visible = False
            Login1.Visible = True
        ElseIf rdlChoice.SelectedValue = 0 AndAlso HttpContext.Current.User.Identity.IsAuthenticated Then
            CustomerInfo1.Visible = False
            Login1.Visible = False
        ElseIf rdlChoice.SelectedValue = 1 Then
            CustomerInfo1.Visible = True
            Login1.Visible = False
        End If
    End Sub
    Protected Sub rdlChoice2_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles rdlChoice2.SelectedIndexChanged
        If rdlChoice2.SelectedValue = 0 Then
            CreditCard1.Visible = True
            ddlCardList.Visible = False
        ElseIf rdlChoice2.SelectedValue = 1 AndAlso ddlCardList.Items.Count > 0 Then
            CreditCard1.Visible = False
            ddlCardList.Visible = True
        ElseIf rdlChoice2.SelectedValue = 1 AndAlso ddlCardList.Items.Count < 0 Then
            rdlChoice2.SelectedValue = 0
            CreditCard1.Visible = True
            ddlCardList.Visible = False
        End If
    End Sub
    Protected Sub wizCheckout_NextButtonClick(sender As Object, e As System.Web.UI.WebControls.WizardNavigationEventArgs) Handles wizCheckout.NextButtonClick
        Select Case wizCheckout.ActiveStepIndex
            Case 0
                If CustomerInfo1.Visible Then
                    CustomerInfo1.Validate()
                    CustomerInfo1.Save()
                    If IsNothing(Session("UserObject")) Then
                        e.Cancel = True
                    Else
                        Response.Redirect("Checkout.aspx")
                    End If
                End If
            Case 1
                If CreditCard1.Visible Then
                    CreditCard1.Validate()
                    CreditCard1.Save()
                    ddlCardList.DataBind()
                    If ddlCardList.Items.Count <= 0 Then
                        e.Cancel = True
                    Else
                        ddlCardList.SelectedIndex = ddlCardList.Items.Count - 1
                    End If
                End If
        End Select
    End Sub

    Protected Sub wizCheckout_PreviousButtonClick(sender As Object, e As System.Web.UI.WebControls.WizardNavigationEventArgs) Handles wizCheckout.PreviousButtonClick
        Select Case wizCheckout.ActiveStepIndex
            Case 1
                'No need to go back to choices, should already be logged in
                e.Cancel = True
        End Select
    End Sub

    Protected Sub wizCheckout_FinishButtonClick(sender As Object, e As System.Web.UI.WebControls.WizardNavigationEventArgs) Handles wizCheckout.FinishButtonClick
        'Move shopping cart to checkout variable
        Session("Checkedout") = New CartProducts()
        CType(Session("Checkedout"), CartProducts).ProductCollection = CType(Session("MyCart"), CartProducts).GetCartList()
        CType(Page.Master.FindControl("lblCartSummary"), Label).Text = CType(Session("MyCart"), CartProducts).GetItemCount()
        pnlConfirm.Visible = True
        lblConfirmation.Text = UserCart.CompleteOrder(Integer.Parse(lblMyCustomerID.Text), Integer.Parse(ddlCardList.SelectedValue))
        System.Threading.Thread.Sleep(3000)
        wizCheckout.Visible = False
        UserCart.EmptyCart()
    End Sub

    Protected Sub ddlCardList_DataBound(sender As Object, e As System.EventArgs) Handles ddlCardList.DataBound
        If ddlCardList.Items.Count <= 0 Then
            rdlChoice2.SelectedValue = 0
            rdlChoice2.Items(1).Enabled = False
            ddlCardList.Visible = False
        Else
            If Not Page.IsPostBack Then
                rdlChoice2.SelectedValue = 1
                rdlChoice2.Items(1).Enabled = True
                ddlCardList.Visible = True
                CreditCard1.Visible = False
            End If
            For Each item As ListItem In ddlCardList.Items
                Dim str As String() = item.Text.Split("|".ToCharArray)
                If str.Length > 1 Then
                    item.Text = str(0) & Utility.GetMaskedString(str(1), 4)
                Else
                    item.Text = Utility.GetMaskedString(item.Text, 4)
                End If
            Next
            End If
    End Sub

End Class
